﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using System.Drawing.Imaging;
using DevExpress.Web.ASPxTabControl;
using DevExpress.Web.ASPxUploadControl;
using DevExpress.Web.ASPxHtmlEditor;
using DevExpress.Web.ASPxCallback;
using Indeco.G24.Web;
using Indeco.G24.Web.Data;
using Indeco.G24.Data;
using Indeco.G24.Services;
using Indeco.G24.Entities; 

namespace Indeco.G24.Website.News.Admin
{
    public partial class TinTucTheoChuDe : System.Web.UI.Page
    {
        TinTucTheoChuDeService tintheochudeservice = new TinTucTheoChuDeService();      
        protected void Page_Load(object sender, EventArgs e)
        {           
            loadData();
        }
        private void loadData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("TenChuDe", typeof(string));
            dt.Columns.Add("TieuDe", typeof(string));
            dt.Columns.Add("IDTinTucChuDe", typeof(string));
            dt.Columns.Add("IDTinTuc", typeof(string));
            TList<Indeco.G24.Entities.TinTucTheoChuDe> tin = tintheochudeservice.GetAll();
            if (tin != null && tin.Count > 0)
            {
                int i = 0;
                foreach (Indeco.G24.Entities.TinTucTheoChuDe t in tin)
                {
                    dt.Rows.Add(new object[] { i, getTenChuDe(t.IDTinTucChuDe.ToString()), getTieuDe(t.IDTinTuc.ToString()), t.IDTinTucChuDe.ToString(), t.IDTinTuc.ToString() });
                    i++;
                }
            }
            ASPxGridView3.DataSource = dt;
            ASPxGridView3.KeyFieldName = "ID";
            ASPxGridView3.DataBind();
        }
        private string getTenChuDe(string id)
        {
            TinTucChuDeService sevice = new TinTucChuDeService();
            Indeco.G24.Entities.TinTucChuDe tin = sevice.Find("id_tintucchude='"+id+"'")[0];
            if (tin != null && !string.IsNullOrEmpty(tin.TenChuDe))
                return tin.TenChuDe;
            else
                return null;
        }
        private string getTieuDe(string id)
        {
            TinTucService sevice = new TinTucService();
            Indeco.G24.Entities.TinTuc tin = sevice.Find("id_tintuc='" + id + "'")[0];
            if (tin != null && !string.IsNullOrEmpty(tin.TieuDe))
                return tin.TieuDe;
            else
                return null;
        }

        protected void ASPxGridView3_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
        {
           
        }

        protected void ASPxGridView3_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            ASPxGridView grid = sender as ASPxGridView;
            ASPxComboBox img = grid.FindEditRowCellTemplateControl((GridViewDataColumn)grid.Columns["TenChuDe"], "TenChuDe") as ASPxComboBox;
            throw new InvalidOperationException("Không thể xóa bản ghi"+img.Value.ToString());  
        }

        protected void ASPxGridView3_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            ASPxGridView grid = sender as ASPxGridView;
            ASPxComboBox chude = grid.FindEditRowCellTemplateControl((GridViewDataColumn)grid.Columns["TenChuDe"], "TenChuDe") as ASPxComboBox;
            ASPxComboBox tieude = grid.FindEditRowCellTemplateControl((GridViewDataColumn)grid.Columns["TieuDe"], "TieuDe") as ASPxComboBox;
            if (chude != null && tieude != null)
            {
                try {
                        Indeco.G24.Entities.TinTucTheoChuDe tin = new Indeco.G24.Entities.TinTucTheoChuDe();
                        Guid idtin=new Guid(tieude.Value.ToString());
                        Guid idchu = new Guid(chude.Value.ToString());
                        tin.IDTinTuc = idtin;
                        tin.IDTinTucChuDe = idchu;
                        bool check=tintheochudeservice.DeepSave(tin);
                        e.Cancel = true;
                        grid.CancelEdit();
                        loadData();
                        if(!check)
                           throw new InvalidOperationException("Không chèn bản ghi");  
                }
                catch {                 
                        throw new InvalidOperationException("Không chèn bản ghi");  
                }
               
            }
            else
            throw new InvalidOperationException("Không chèn bản ghi");  
        }

        protected void ASPxGridView3_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            try
            {
                Indeco.G24.Entities.TinTucTheoChuDe tin = new Indeco.G24.Entities.TinTucTheoChuDe();
                Guid idtin = new Guid(e.Values["IDTinTuc"].ToString());
                Guid idchu = new Guid(e.Values["IDTinTucChuDe"].ToString());
                tin.IDTinTuc = idtin;
                tin.IDTinTucChuDe = idchu;
                bool check = tintheochudeservice.Delete(tin);
                e.Cancel = true;              
                loadData();
                if (!check)
                    throw new InvalidOperationException("Không xóa bản ghi");
            }
            catch
            {
                throw new InvalidOperationException("Không xóa bản ghi");
            }            
        }

        protected void ASPxGridView3_DataBinding(object sender, EventArgs e)
        {
           
        }
    }
}
